<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <title>canvas</title>
    <style>
        body{
            padding:0;
            margin:0;
        }
        canvas{
            background:#eee;
            padding:0;
        }
        div{
            color: #000;
            background: #ddd;
            font-size: 16px;
        }
         p{
            position: absolute;
            left:420px; top: 50px;
            padding: 10px 30px;
            color: white;
            font-size: 20px;
            font-family: "Microsoft Yahei";
            
        }
        #canvas{
            cursor: pointer;
            background:#eee;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script src='../js/ball.js'></script>
    <script src="../js/line.js"></script>
    <script src='../js/utils.js'></script>
    <script>
        window.onload = function(){
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext('2d');
            canvas.width = document.body.clientWidth;
            canvas.height = document.body.scrollHeight;

            var ball = new Ball(40, "red"),
               mouse = utils.captureMouse(canvas);
           
           var xpos = 0,                 //物体的3D坐标
               ypos = 0,
               zpos = 0,
               fl = 250,                 //距离屏幕的距离（焦距）
               vpX = canvas.width/2,    //消失点
               vpY = canvas.height/2;
           
           window.addEventListener('keydown', function(e){
               console.log(e.keyCode)
               if(e.keyCode === 38){ //up
                   zpos += 5;
               }else if(e.keyCode === 40){
                   zpos -= 5;
               }
           }, false);
           
           (function drawFrame(){
               window.requestAnimationFrame(drawFrame, canvas);
               context.clearRect(0, 0, canvas.width, canvas.height);
               
               if(zpos > -fl){
                    var scale = fl/(fl + zpos);       //缩放比列
                           xpos = mouse.x - vpX;
                           ypos = mouse.y - vpY;
                   ball.scaleX = ball.scaleY = scale; //物体大小变化
                   ball.x = vpX + xpos*scale;          //新坐标
                   ball.y = vpY + ypos*scale;
                   ball.visible = true;                 //物体可见
               }else{
                   ball.visible = false
               }
              
               if(ball.visible){
                   ball.draw(context);
             }
           }())     
        }
        

        // 物体大小和fl 和 z 之间的关系
        // scale = f1/(fl+z)=1/(1+z/f1);

        //context.scale(this.scalex,this.scaley);
    </script>
</body>
</html>
